<?php

namespace App\Http\Controllers\Mini;

use App\Models\Carts;
use App\Models\Goods;
use App\Models\Stores;
use App\Models\Users;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class CartController extends Controller
{
    /**
     * 购物车列表
     * @route api.cart.cartLists
     * @param user_id 用户id
     * @param page 页码
     * @param limit 条数
     */
    public function cartLists(Request $request){
        try{
            $user_id = (int)$request->post("user_id",1);//用户id
            $page = (int)$request->get('page',1);//页码
            $limit = (int)$request->get('limit',5);//条数
            if (empty($user_id) || empty($page)) return response()->json(["code"=>0, "msg"=>'参数错误']);
            $userinfo = Users::where(['id'=>$user_id, 'status'=>1])->value('type');
            if (empty($userinfo)) return response()->json(["code"=>0, "msg"=>'用户不存在']);
            $lists = Carts::allLists(['user_id'=>$user_id], ['*'], $page, $limit)->toArray();
            if (!empty($lists['data'])){
                //购物车商品以店铺ID分组显示
                foreach ($lists['data'] as $data){
                    $store_cart_list[$data['store_id']]['store_name'] = $data['store_name'];
                    $store_cart_list[$data['store_id']]['goods_list'][] = $data;
                }
                foreach ($store_cart_list as $cart){
                    $carts[] = $cart;
                }
                $lists['data'] = $carts;
            }

            return response()->json([
                "code" => 1,
                "msg" => "获取成功",
                "data" => $lists
            ]);
        }catch (\Exception $e){
            return response()->json(["code"=>0, "msg"=>$e->getMessage()]);
        }
    }

    /**
     * 添加购物车或减少购物车数量
     * @route api.cart.cartEdit
     * @param user_id 用户id
     * @param store_id 店铺id
     * @param goods_id 商品id
     * @param num 数量
     * @param type 类型 1.增加 2.减少
     */
    public function cartEdit(Request $request) {
        try{
            $user_id = (int)$request->post("user_id");
            $store_id = (int)$request->post("store_id");
            $goods_id = (int)$request->post("goods_id");
            $num = (int)$request->post("num");
            $type = (int)$request->post("type");//类型 1.增加 2.减少
            if (empty($user_id) || empty($store_id) || empty($goods_id) || empty($num) || empty($type)){
                return response()->json(["code"=>0, "msg"=>'参数错误']);
            }
            $user_info = Users::where(['id'=>$user_id, 'status'=>1])->value('type');
            if (empty($user_info)) return response()->json(["code"=>0, "msg"=>'用户不存在']);
            if ($user_info != 1) return response()->json(["code"=>0, "msg"=>'供应商不能购买商品']);
            $store_name = Stores::where('id',$store_id)->value('store_name');
            $goods_info = Goods::where('id',$goods_id)->first(['title','thumb','price']);

            //查询购物车是否已经存在同一用户 同一规格 同一商品
            $cart = Carts::where(['user_id' => $user_id,'goods_id' => $goods_id,'store_id' => $store_id])->first();
            if (!empty($cart)) {
                if ($type == 1) $cart->num = $cart["num"] + $num;
                if ($type == 2) {
                    if ($cart["num"] > 1) {
                        $cart->num = $cart["num"] - $num;
                    } else {
                        return response()->json(["code"=>0, "msg"=>'只有一个，不能再减了']);
                    }
                }
            } else {
                if ($type == 1) {
                    $cart = new Carts();
                    $cart->user_id = $user_id;
                    $cart->store_id = $store_id;
                    $cart->store_name = $store_name;
                    $cart->goods_id = $goods_id;
                    $cart->goods_name = $goods_info['title'];
                    $cart->goods_img = $goods_info['thumb'];
                    $cart->goods_price = $goods_info['price'];
                    $cart->num = $num;
                } else {
                    return response()->json(["code"=>0, "msg"=>'参数错误']);
                }
            }
            $cart->save();
            return response()->json(["code"=>1, "msg"=>'操作成功']);
        }catch (\Exception $e){
            return response()->json(["code"=>0, "msg"=>$e->getMessage()]);
        }
    }

    /**
     * 删除购物车
     * @route api.cart.cartDelete
     * @param user_id 用户id
     * @param cart_id 购物车id
     */
    public function cartDelete(Request $request) {
        $user_id = (int)$request->post("user_id");
        $cart_id = $request->post("cart_id");
        if (empty($user_id) || empty($cart_id)) return response()->json(["code"=>0, "msg"=>"参数错误"]);
        $ids = explode(",", $cart_id);
        foreach ($ids as $key => $val){
            Carts::where(['id'=>$val,'user_id'=>$user_id])->delete();
        }
        return response()->json(["code"=>1, "msg"=>'操作成功']);
    }

}